﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Common;
using ESOL.Business.Sec;
using ESOL.Business.Mst;

namespace ESOL.WinUI.Sec
{
    public partial class frmMaintainRole : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        public SecRoleEntity RoleEntity { get; set; }
        #endregion

        #region Constructor
        public frmMaintainRole()
        {
            InitializeComponent();
        }
        #endregion

        #region Funtions
        private bool CheckInputData()
        {
            if (string.IsNullOrEmpty(txtRoleName.Text))
            {
                MessageBox.Show("角色名称不能为空!","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtRoleName.Focus();
                return false;
            }
            if (cmbRoleCategory.SelectedValue == null)
            {
                MessageBox.Show("角色范畴不能为空!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cmbRoleCategory.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtExternalUserRole.Text))
            {
                MessageBox.Show("拓展角色不能为空!","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtExternalUserRole.Focus();
                return false;
            }
            return true;
        }

        private void BindCmbParentRole()
        {
            List<SecRoleEntity> list = SecRoleBs.GetRolesAllList();
            cmbParentId.DataSource = list;
            SecRoleEntity role = new SecRoleEntity();
            list.Insert(0, role);
            cmbParentId.DisplayMember = "RoleName";
            cmbParentId.ValueMember = "RoleId";

            UICommon.UICommonFunctions.BindComboboxByEnum(cmbRoleCategory, typeof(RoleCategory), typeof(Decimal), false);
        }

        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        protected override void Click_Cancel()
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void SetFields()
        {
            if (RoleEntity == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(RoleEntity.ParentId) == false)
            {
                cmbParentId.SetSelectedValue(RoleEntity.ParentId);
            }
            txtRoleType.Text = RoleEntity.RoleType;
            txtRoleName.Text = RoleEntity.RoleName;
            cmbRoleCategory.SetSelectedValue(RoleEntity.RoleCategory);
            txtExternalUserRole.Text = RoleEntity.ExternalUserRole.ToString();
            MstPersonEntity personentity = new MstPersonEntity();
            personentity = MstPersonBs.GetMstPersonByID(RoleEntity.LastModifiedBy);
            if (personentity != null)
            {
                txtLastModifiedBy.Text = personentity.NameCn;
            }
            txtLastModifiedTime.Text = RoleEntity.LastModifiedTime.ToString("yyyy-MM-dd hh:mm:ss");
            txtRemark.Text = RoleEntity.Remark;
        }

        private void SetEntity()
        {
            if (RoleEntity == null)
            {
                RoleEntity = new SecRoleEntity();
                RoleEntity.RoleId = Guid.NewGuid().ToString();
            }
            RoleEntity.ParentId = null;
            if (cmbParentId.SelectedValue != null && cmbParentId.SelectedValue.ToString() != "")
            {
                RoleEntity.ParentId = cmbParentId.SelectedValue.ToString();
            }
            RoleEntity.RoleType = txtRoleType.Text;
            RoleEntity.RoleName = txtRoleName.Text;
            RoleEntity.RoleCategory = CommonFunctions.ToDecimal(cmbRoleCategory.SelectedValue);
            RoleEntity.ExternalUserRole = CommonFunctions.ToDecimal(txtExternalUserRole.Text);
            RoleEntity.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
            RoleEntity.LastModifiedTime = DateTime.Now;
            RoleEntity.Remark = txtRemark.Text;
        }

        private bool Save()
        {
            try
            {
                if (CheckInputData()==false)
                {
                    return false;
                }
                SetEntity();
                if (ThisActionType == ActionType.New)
                {
                    bool isSuccessful = SecRoleBs.InsertSecRole(RoleEntity);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("Insert failed!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    bool isSuccessful = SecRoleBs.UpdateSecRole(RoleEntity);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("Modify failed!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.View)
                {
                }
                else if (ThisActionType == ActionType.Approve)
                {
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region Event
        private void frmMaintainRole_Load(object sender, EventArgs e)
        {
            try
            {
                BindCmbParentRole();
                SetFields();
            }
            catch (Exception ex)
            {
            }
        }
        #endregion
    }
}
